package run.siyuan.jdk.新特性;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author siyuan
 * @description: TODO LocalDateTime 操作
 * @date 2021/5/11 10:31 上午
 */
public class LocalDateTimeTest {

    @Test
    public void test1(){
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间"+now);
        // 获取指定日期
        LocalDateTime dateTime = LocalDateTime.of(2021, 01, 01, 00, 00, 00);
        System.out.println(dateTime);
        //
        System.out.println(dateTime.toString());
        System.out.println(dateTime.toLocalDate());
        System.out.println(dateTime.toLocalTime());
        System.out.println(dateTime.getYear());
        System.out.println(dateTime.getMonth());
        System.out.println(dateTime.getDayOfWeek());
        System.out.println(dateTime.getDayOfMonth());
        System.out.println(dateTime.getDayOfYear());
        System.out.println(dateTime.getHour());
        System.out.println(dateTime.getMinute());
        System.out.println(dateTime.getSecond());

        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        System.out.println(dateTime.format(pattern));
        // 时间运算
        System.out.println(dateTime.plusYears(2));
        System.out.println(dateTime.minusYears(2));

        System.out.println(dateTime.plusMonths(2));
        System.out.println(dateTime.minusMinutes(2));

        System.out.println(dateTime.plusDays(2));
        System.out.println(dateTime.minusDays(2));

        // 获取毫秒值
        System.out.println(dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        // 获取秒
        System.out.println(dateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());


        // 获取指定时间
        LocalDateTime dateTime1 = LocalDateTime.of(2021, 01, 01, 00, 00, 00);
        LocalDateTime dateTime2 = LocalDateTime.of(2021, 02, 01, 00, 00, 00);

        System.out.println(dateTime1.isBefore(dateTime2));
        System.out.println(dateTime1.isAfter(dateTime2));




    }

}
